package com.ruoyi.info.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.info.domain.ProductForm;
import com.ruoyi.info.domain.Region;
import com.ruoyi.info.service.IProductFormService;
import com.ruoyi.info.service.IRegionService;
import com.ruoyi.info.vo.AuditingEnterpriseBase;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.EnterpriseBase;
import com.ruoyi.info.service.IEnterpriseBaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 企业基地Controller
 *
 * @author wangshi
 * @date 2023-06-06
 */
@RestController
@RequestMapping("/info/base")
public class EnterpriseBaseController extends BaseController {
    @Autowired
    private IEnterpriseBaseService enterpriseBaseService;

    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private IProductFormService iProductFormService;

    @Autowired
    private IRegionService iRegionService;

    /**
     * 查询企业基地列表
     */
    @PreAuthorize("@ss.hasPermi('info:base:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseBase enterpriseBase) {
        startPage();
        List<EnterpriseBase> list = enterpriseBaseService.selectEnterpriseBaseList(enterpriseBase);
        return getDataTable(list);
    }

    /**
     * 查询企业基地待审核列表
     */
    @PreAuthorize("@ss.hasPermi('info:base:auditingList')")
    @GetMapping("/auditing/list")
    public TableDataInfo auditingList(EnterpriseBase enterpriseBase) {
        startPage();
        enterpriseBase.setEbState("0");
        List<EnterpriseBase> list = enterpriseBaseService.selectEnterpriseBaseList(enterpriseBase);
        return getDataTable(list);
    }

    /**
     * 导出企业基地列表
     */
    @PreAuthorize("@ss.hasPermi('info:base:export')")
    @Log(title = "企业基地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterpriseBase enterpriseBase) throws IOException {

        enterpriseBaseService.exportEnterpriseBase(response,enterpriseBase);

//        ExcelUtil<EnterpriseBase> util = new ExcelUtil<EnterpriseBase>(EnterpriseBase.class);
//        util.exportExcel(response, list, "企业基地数据");
    }

    /**
     * 导出企业基地 批量导入模板
     */
    @PreAuthorize("@ss.hasPermi('info:base:templateDownload')")
    @Log(title = "企业基地", businessType = BusinessType.EXPORT)
    @PostMapping("/template/download")
    public void templateDownload(HttpServletResponse response) throws IOException {
        enterpriseBaseService.templateDownload(response);
    }


    @PreAuthorize("@ss.hasPermi('info:base:templateRender')")
    @Log(title = "企业基地", businessType = BusinessType.IMPORT)
    @PostMapping("/template/render")
    public AjaxResult templateRender(MultipartFile file) throws IOException {
        return toAjax(enterpriseBaseService.templateRender(file));

    }

    /**
     * 获取企业基地详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:base:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(enterpriseBaseService.selectEnterpriseBaseById(id));
    }

    /**
     * 新增企业基地
     */
    @PreAuthorize("@ss.hasPermi('info:base:add')")
    @Log(title = "企业基地", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnterpriseBase enterpriseBase) {
        return toAjax(enterpriseBaseService.insertEnterpriseBase(enterpriseBase));
    }

    /**
     * 修改企业基地
     */
    @PreAuthorize("@ss.hasPermi('info:base:edit')")
    @Log(title = "企业基地", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnterpriseBase enterpriseBase) {
        return toAjax(enterpriseBaseService.updateEnterpriseBase(enterpriseBase));
    }

    /**
     * 删除企业基地
     */
    @PreAuthorize("@ss.hasPermi('info:base:remove')")
    @Log(title = "企业基地", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(enterpriseBaseService.deleteEnterpriseBaseByIds(ids));
    }

    /**
     * 企业基地审核
     *
     * @param auditingEnterpriseBase 企业基地审核字段
     * @return
     */
    @PreAuthorize("@ss.hasPermi('info:base:auditing')")
    @Log(title = "企业基地审核", businessType = BusinessType.UPDATE)
    @PostMapping("/auditing")
    public AjaxResult auditing(@RequestBody AuditingEnterpriseBase auditingEnterpriseBase) {
        auditingEnterpriseBase.setCreateBy(getUsername());
        return toAjax(enterpriseBaseService.auditingEnterpriseBase(auditingEnterpriseBase));
    }

    ;

    /**
     * 企业基地全省排名
     *
     * @param name 企业基地名称
     * @return
     */
    @PreAuthorize("@ss.hasPermi('info:base:ranking')")
    @GetMapping("/ranking")
    public AjaxResult selectRanking(String name) {
        return AjaxResult.success(enterpriseBaseService.selectRanking(name));
    }

    ;

    /**
     * 企业供货产品 满产产能趋势
     *
     * @param name 企业基地名称
     * @return
     */
    @PreAuthorize("@ss.hasPermi('info:base:capacity')")
    @GetMapping("/capacity")
    public AjaxResult selectCapacity(String name) {
        return AjaxResult.success(enterpriseBaseService.selectCapacity(name));
    }

    ;

    /**
     * 企业供货产品类型分布&满产产能趋势
     *
     * @param name 企业基地名称
     * @return
     */
    @PreAuthorize("@ss.hasPermi('info:base:productTypeDistribution')")
    @GetMapping("/productTypeDistribution")
    public AjaxResult selectProductTypeDistribution(String name) {
        return AjaxResult.success(enterpriseBaseService.selectProductTypeDistribution(name));
    }

    ;

    /**
     * 企业基地基础信息
     *
     * @param name 企业基地名称
     * @return
     */
    @PreAuthorize("@ss.hasPermi('info:base:basicInfo')")
    @GetMapping("/basicInfo")
    public AjaxResult selectBasicInfo(String name) {
        return AjaxResult.success(enterpriseBaseService.selectBasicInfo(name));
    }

    ;
}
